package cn.lovemmd.model.admin.controller;

import com.alibaba.fastjson2.JSON;
import cn.lovemmd.model.admin.common.PageResult;
import cn.lovemmd.model.admin.common.Result;
import cn.lovemmd.model.admin.dto.ModelDTO;
import cn.lovemmd.model.admin.service.ModelService;
import cn.lovemmd.model.admin.vo.ModelVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * 模型管理控制器V2
 * 使用FastJson2进行JSON序列化
 * 
 * @author TLS
 * @since 2024-01-01
 */
@RestController
@RequestMapping("/v2/model")
@Api(tags = "模型管理V2")
@Validated
@RequiredArgsConstructor
public class ModelControllerV2 {

    private static final Logger log = LoggerFactory.getLogger(ModelControllerV2.class);
    private final ModelService modelService;

    /**
     * 分页查询模型列表
     */
    @GetMapping(value = "/list", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation("分页查询模型列表")
    public ResponseEntity<String> getModelList(
            @ApiParam("当前页码") @RequestParam(defaultValue = "1") Long page,
            @ApiParam("每页大小") @RequestParam(defaultValue = "10") Long size,
            @ApiParam("搜索关键字") @RequestParam(required = false) String search,
            @ApiParam("职业筛选") @RequestParam(required = false) String occ) {
        
        try {
            PageResult<ModelVO> result = modelService.getModelPage(page, size, search, occ);
            Result<PageResult<ModelVO>> response = Result.success(result);
            return ResponseEntity.ok(JSON.toJSONString(response));
        } catch (Exception e) {
            log.error("查询模型列表失败", e);
            Result<String> errorResponse = Result.error("查询模型列表失败：" + e.getMessage());
            return ResponseEntity.ok(JSON.toJSONString(errorResponse));
        }
    }

    /**
     * 根据ID查询模型详情
     */
    @GetMapping(value = "/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation("根据ID查询模型详情")
    public ResponseEntity<String> getModelById(
            @ApiParam("模型ID") @PathVariable @NotNull Long id) {
        
        try {
            ModelVO model = modelService.getModelById(id);
            if (model == null) {
                Result<String> errorResponse = Result.error("模型不存在");
                return ResponseEntity.ok(JSON.toJSONString(errorResponse));
            }
            Result<ModelVO> response = Result.success(model);
            return ResponseEntity.ok(JSON.toJSONString(response));
        } catch (Exception e) {
            log.error("查询模型详情失败", e);
            Result<String> errorResponse = Result.error("查询模型详情失败：" + e.getMessage());
            return ResponseEntity.ok(JSON.toJSONString(errorResponse));
        }
    }

    /**
     * 添加模型
     */
    @PostMapping(value = "/add", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation("添加模型")
    public ResponseEntity<String> addModel(@Valid @RequestBody ModelDTO modelDTO) {
        try {
            boolean success = modelService.addModel(modelDTO);
            Result<String> response = success ? 
                Result.success("添加模型成功") : 
                Result.error("添加模型失败");
            return ResponseEntity.ok(JSON.toJSONString(response));
        } catch (Exception e) {
            log.error("添加模型失败", e);
            Result<String> errorResponse = Result.error("添加模型失败：" + e.getMessage());
            return ResponseEntity.ok(JSON.toJSONString(errorResponse));
        }
    }

    /**
     * 更新模型
     */
    @PostMapping(value = "/update", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation("更新模型")
    public ResponseEntity<String> updateModel(@Valid @RequestBody ModelDTO modelDTO) {
        try {
            boolean success = modelService.updateModel(modelDTO);
            Result<String> response = success ? 
                Result.success("更新模型成功") : 
                Result.error("更新模型失败");
            return ResponseEntity.ok(JSON.toJSONString(response));
        } catch (Exception e) {
            log.error("更新模型失败", e);
            Result<String> errorResponse = Result.error("更新模型失败：" + e.getMessage());
            return ResponseEntity.ok(JSON.toJSONString(errorResponse));
        }
    }

    /**
     * 删除模型
     */
    @DeleteMapping(value = "/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation("删除模型")
    public ResponseEntity<String> deleteModel(
            @ApiParam("模型ID") @PathVariable @NotNull Long id) {
        
        try {
            boolean success = modelService.deleteModel(id);
            Result<String> response = success ? 
                Result.success("删除模型成功") : 
                Result.error("删除模型失败");
            return ResponseEntity.ok(JSON.toJSONString(response));
        } catch (Exception e) {
            log.error("删除模型失败", e);
            Result<String> errorResponse = Result.error("删除模型失败：" + e.getMessage());
            return ResponseEntity.ok(JSON.toJSONString(errorResponse));
        }
    }

    /**
     * 批量删除模型
     */
    @DeleteMapping(value = "/batch", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation("批量删除模型")
    public ResponseEntity<String> deleteBatchModels(@RequestBody List<Long> ids) {
        try {
            boolean success = modelService.deleteBatchModels(ids);
            Result<String> response = success ? 
                Result.success("批量删除模型成功") : 
                Result.error("批量删除模型失败");
            return ResponseEntity.ok(JSON.toJSONString(response));
        } catch (Exception e) {
            log.error("批量删除模型失败", e);
            Result<String> errorResponse = Result.error("批量删除模型失败：" + e.getMessage());
            return ResponseEntity.ok(JSON.toJSONString(errorResponse));
        }
    }

    /**
     * 获取模型统计信息
     */
    @GetMapping(value = "/stats", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation("获取模型统计信息")
    public ResponseEntity<String> getModelStats() {
        try {
            Object stats = modelService.getModelStats();
            Result<Object> response = Result.success(stats);
            return ResponseEntity.ok(JSON.toJSONString(response));
        } catch (Exception e) {
            log.error("获取模型统计信息失败", e);
            Result<String> errorResponse = Result.error("获取模型统计信息失败：" + e.getMessage());
            return ResponseEntity.ok(JSON.toJSONString(errorResponse));
        }
    }

    /**
     * 获取职业列表
     */
    @GetMapping(value = "/occupations", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation("获取职业列表")
    public ResponseEntity<String> getOccupationList() {
        try {
            List<Object> occupations = modelService.getOccupationList();
            Result<List<Object>> response = Result.success(occupations);
            return ResponseEntity.ok(JSON.toJSONString(response));
        } catch (Exception e) {
            log.error("获取职业列表失败", e);
            Result<String> errorResponse = Result.error("获取职业列表失败：" + e.getMessage());
            return ResponseEntity.ok(JSON.toJSONString(errorResponse));
        }
    }
}
